package net.arunoday.activiti.demo.web.defect;

import net.arunoday.activiti.demo.entity.Defect;
import net.arunoday.activiti.demo.service.DefectService;
import net.arunoday.activiti.demo.web.BasePage;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * 
 * @author Aparna Chaudhary (aparna.chaudhary@gmail.com)
 */
@SuppressWarnings("serial")
public class ReviewDefectPage extends BasePage {

	@SpringBean
	private DefectService defectService;

	private Defect defect;
	private Long defectId;

	public ReviewDefectPage() {
		this(null);
	}

	public ReviewDefectPage(Long id) {
		this.defect = defectService.getDefectById(id);
		this.defectId = defect.getId();

		FeedbackPanel msgs = new FeedbackPanel("msgs");
		add(msgs);

		Form<Defect> form = new Form<Defect>("form") {
			protected void onSubmit() {
				Defect defect = ReviewDefectPage.this.defect;
				defect = defectService.reviewDefect(defectId,
						defect.getAssignedTo());
				setResponsePage(new DefectDetailPage(defect.getId()));
			}
		};
		form.setModel(new CompoundPropertyModel<Defect>(defect));

		TextField<String> description = new TextField<String>("description");
		description.setRequired(true);
		description.setEnabled(false);
		form.add(description);

		TextField<String> assignedTo = new TextField<String>("assignedTo");
		assignedTo.setRequired(true);
		form.add(assignedTo);

		add(form);

	}

	@Override
	protected IModel<String> getTitleModel() {
		return new Model<String>("Create / Edit Defect");
	}

}
